package me.raih.util;

import me.raih.pojo.po.Menu;

import java.util.*;

/**
 * @Author Raih
 * @Time 2020/11/26  12:49
 *
 * 菜单重组工具
 */
public class MenuUtil {

    /**
     * 重组成新的菜单列表
     * @param menuList
     * @return List<Map<Menu,List<Menu>>>
     */
    public static List<Map<List<Menu>,List<Menu>>> recombination(List<Menu> menuList){
        //定义输出数组
        List<Map<List<Menu>,List<Menu>>> outside = new ArrayList<>();
        //大菜单集合
        List<Menu> bigMenu = new ArrayList<>();

        //获取大菜单
        for (Menu value : menuList) {
            if (value.getFid() == 0) {
                bigMenu.add(value);
            }
        }

        //for (Menu menu : menuList) {
        //    int temp = 0;
        //    for (int i = 0; i < menuList.size(); i++) {
        //
        //        if(menuList.get(i).getFid() == 0){
        //
        //        }
        //
        //    }
        //}

        //将大菜单从所有菜单中剔除出去
        for (Menu menu : bigMenu) {
            menuList.remove(menu);
        }

        //遍历剩下的所有小菜单
        for (Menu menu : bigMenu) {
            List<Menu> littleMenu = new ArrayList<>();
            Map<List<Menu>,List<Menu>> insideMap = new HashMap<>();
            //作为Key的  大菜单
            List<Menu> insideMenu = new ArrayList<>();
            for (Menu value : menuList) {
                //如果小菜单的父类是大菜单的权重
                if (value.getFid().equals(menu.getWeight())) {
                    littleMenu.add(value);
                }
            }
            insideMenu.add(menu);
            //遍历结束之后添加到 K，V 里面
            insideMap.put(insideMenu,littleMenu);
            outside.add(insideMap);
        }

        //System.out.println(outside);

        //return outside;
        return outside;
    }

    public static List<Menu> menuOrder(List<Menu> menuList){
        //List<Menu> menus = new ArrayList<>();
        for (int i = 0; i < menuList.size()-1; i++) {
            for (int j = 0; j < menuList.size()-1-i; j++) {
                if(menuList.get(j).getWeight() > menuList.get(j+1).getWeight()){
                    //Menu menu = menuList.get(j);
                    Collections.swap(menuList,j,j+1);
                }
            }
        }
        return menuList;
    }

    private static List<Map<Integer,List<Menu>>> one(List<Menu> menuList){
        List<Map<Integer,List<Menu>>> outside = new ArrayList<>();
        int temp = 0;
        for (Menu menu : menuList) {
            //内部存放每个大-跟小菜单
            List<Menu> insideList = new ArrayList<>();
            Map<Integer,List<Menu>> minMap = new HashMap<>();
            //首菜单没错了
            if(menu.getFid() == 0){
                insideList.add(menu);
                for (int i = 0; i < menuList.size(); i++) {
                    //如果找到了自己的子菜单
                    if (menu.getWeight().equals(menuList.get(i).getFid())) {
                        insideList.add(menuList.get(i));
                    }
                }
                minMap.put(temp,insideList);
                temp++;
            }
            if(minMap.size()>0){

                outside.add(minMap);
            }
        }
        return outside;
    }

}
